package cn.gailvlun.gll.presentation.apartment.room;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.bigkoo.pickerview.OptionsPickerView;

import java.util.Arrays;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivityMatchProfileBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.Member;
import cn.gailvlun.gll.net.apartment.member.AddMemberReq;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.ProfileUtil;
import cn.gailvlun.gll.util.RxUtil;
import cn.gailvlun.gll.widget.NavigationBarView;



public class MatchProfileActivity extends BaseActivity {

    public static void openActivity(Context context, int roomId) {
        Intent intent = new Intent(context, MatchProfileActivity.class);
        intent.putExtra("roomId", roomId);
        context.startActivity(intent);
    }

    private ActivityMatchProfileBinding mBinding;

    private int mRoomId;

    public enum SEX {
        HOMO, HETERO;

        @Override
        public String toString() {
            switch (this) {
                case HOMO: {
                    return "同性";
                }
                case HETERO: {
                    return "异性";
                }
                default: {
                    return "程序出错";
                }
            }
        }
    }

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_match_profile);
        mBinding.setPresenter(new Presenter());
        mBinding.setSexType(SEX.HETERO);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        mRoomId = getIntent().getIntExtra("roomId", -1);
        if (mRoomId == -1) {
            finish();
        }
    }

    public class Presenter {
        public void onSex(SEX selectedSex) {
            OptionsPickerView<SEX> optionsPickerView = new OptionsPickerView.Builder(mContext, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    mBinding.setSexType(SEX.values()[options1]);
                }
            })








                    .build();
            optionsPickerView.setPicker(Arrays.asList(SEX.values()));
            if (selectedSex == null) {
                optionsPickerView.setSelectOptions(SEX.HETERO.ordinal());
            } else {
                optionsPickerView.setSelectOptions(selectedSex.ordinal());
            }
            optionsPickerView.show();
        }

        public void onMatch() {
            AddMemberReq addMemberReq = new AddMemberReq(mRoomId);
            addMemberReq.setM_homosex(mBinding.getSexType() == SEX.HOMO);
            addMemberReq.setM_introduce(mBinding.tvIntroduce.getText().toString());
            addMemberReq.setM_gender(ProfileUtil.getProfile().isGender());
            HttpMethods.getMemberService()
                    .addMember(addMemberReq)
                    .compose(RxUtil.<Member>applyScheduler())
                    .subscribe(new HttpSubscriber<Member>() {
                        @Override
                        protected void onSuccess(Member response) {
                            
                            MatchActivity.openActivity(mContext, response.getId(), response.getRoom());
                            finish();
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable Member response, int code) {
                            DialogUtil.showError(mContext, errMsg);
                        }
                    });
        }
    }

    public static class Util {
        public static String getSexType(SEX sexType) {
            if (sexType == null) {
                return "";
            }
            return sexType.toString();
        }
    }
}
